//
//  OrderPayVC.m
//  KuWoCan
//
//  Created by hwd on 17/4/7.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "OrderPayVC.h"
#import "NwOrderRequest.h"
#import "PhotoManageCell.h"
#import "OtherCostListModel.h"
#import "PicPageListModel.h"
@interface OrderPayVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIImageView *payImg;

@property (nonatomic,strong) NSString *imgUrl;

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tabView;

@property (nonatomic,copy) NSMutableArray *picArrFirst;

@property (nonatomic,strong) NSMutableArray *picArr;

@end

@implementation OrderPayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"确认支付";
    self.view.backgroundColor = KwBgColor;
    _picArrFirst = [[NSMutableArray alloc]init];
    [self getPicData];

}
-(void)getPicData
{
    
    NWPicManageRequest *picRequest = [[NWPicManageRequest alloc] init];
    [picRequest startRequestQueryPicWithProcessId:_orderId pageNo:@"1" pageSize:@"10" type:[NSString stringWithFormat:@"%zd", 13]];
    [picRequest completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (succ)
        {
            PicPageListModel *pageModel = [[PicPageListModel alloc] initWithDictionary:result[@"pageList"] error:nil];
            if ([pageModel.count integerValue] > 0)
            {
                self.picArr = pageModel.list.mutableCopy;

                [_picArrFirst removeAllObjects];
                for (PicListModel *PicModel in self.picArr.mutableCopy)
                {
                    [_picArrFirst addObject:PicModel];
                }
            }
            
        }
            [self setUpUI];
    }];
    

}
- (void)setUpUI
{
    //创建列表
    _tabView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    _tabView.backgroundColor = KwBgColor;
    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabView.estimatedRowHeight = 40.0;
    _tabView.rowHeight = UITableViewAutomaticDimension;
    _tabView.delegate = self;
    _tabView.dataSource = self;
    [self.view addSubview:_tabView];
    [_tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@-55);
    }];
    
    //创建底部按钮
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [confirmBtn setBackgroundColor:KwBlueColor];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn setTitle:@"提交" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = KwFont(16.0);
    [confirmBtn addTarget:self action:@selector(uploadImageWithImages) forControlEvents:UIControlEventTouchUpInside];
    confirmBtn.layer.masksToBounds = YES;
    confirmBtn.layer.cornerRadius = 5.0;
    [self.view addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@-5);
        make.top.equalTo(_tabView.mas_bottom).offset(1);
        make.left.equalTo(@5);
        make.right.equalTo(@-5);
    }];
    
}
#pragma mark tableViewDelegate,DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *photoManageCell_ID = @"PhotoManageCell";
        PhotoManageCell *cell;
        cell = [tableView dequeueReusableCellWithIdentifier:photoManageCell_ID];
        if (!cell)
        {
            cell = [[PhotoManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:photoManageCell_ID];
            
        }
        
        cell.vc = self;
        cell.titleLabel.text = @"支付截图管理";
        [cell configCell:_picArr];
        cell.orderId = _orderId;
        cell.picType = 13;
        cell.isSubmit = NO;
        cell.isChange = YES;
        cell.choosePictureHandle = ^(NSMutableArray<PicListModel *> *PicArr) {
        
//            //移除已加入的NSDictionary
//            for (int i = (int)(PicArr.count-1); i>=0; i--) {
//                if ([PicArr[i] isKindOfClass:[NSDictionary class]]) {
//                    [PicArr removeObjectAtIndex:i];
//                }
//            }
            //添加新加入的PicListModel
            for (PicListModel *PicModel in PicArr.mutableCopy)
            {
                [_picArrFirst addObject:@{@"title":PicModel.title,@"imgs":PicModel.imgs}];
            }
        };
        return cell;
    
}


- (void)uploadImageWithImages
{
    if (_picArrFirst.count<=0) {
        [SVProgressHUD showErrorWithStatus:@"请先选择图片"];
        return;
    }

//NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"processId":_orderId,@"type":[NSString stringWithFormat:@"%ld",(long)_picType],@"kwPhotoList":[CommonTool beJsonStrWithObj:_picMutableArr]}];
//NWPicManageRequest *picRequest = [[NWPicManageRequest alloc] init];
//[picRequest startRequestAddOnePicWithDic:dic];
    
    //model类转化为字典
    NSMutableArray *tiJIaoArr = [NSMutableArray new];
    for (int i = (int)(_picArrFirst.count-1); i>=0; i--) {
        if ([_picArrFirst[i] isKindOfClass:[PicListModel class]]) {
            PicListModel *PicModel = _picArrFirst[i];
            [tiJIaoArr addObject:@{@"title":PicModel.title,@"imgs":PicModel.imgs}];
        }else
        {
            [tiJIaoArr addObject:_picArrFirst[i]];
        }
    }

    NSMutableDictionary *paramateDic = [[NSMutableDictionary alloc]init];
    [paramateDic setObject:_orderId?_orderId:@"" forKey:@"processId"];
    [paramateDic setObject:@"13" forKey:@"type"];
    [paramateDic setValue:[CommonTool beJsonStrWithObj:tiJIaoArr] forKey:@"kwPhotoList"];
    
    NWPicManageRequest *picRequest = [[NWPicManageRequest alloc] init];
    [picRequest startRequestChangeOrAddAllPicWithDic:paramateDic];
    [picRequest completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (succ)
        {
            [self uploadPayPic];
        }else{
            
        }
    }];
}

- (void)uploadPayPic
{
        __weak typeof(self) weakSelf = self;

        NwOrderRequest *request = [[NwOrderRequest alloc] init];
        [request startRequestPassOrdersWithOrderId:_orderId userId:[Singleton shareInstance].userId orderStatus:[NSString stringWithFormat:@"%ld",(long)OrderQueryStatusToPay] approvalType:@"0" refuseReason:@"" paymentProof:@""];
        [request completion:^(id result, NSString *message, id extension, BOOL succ) {
            if (succ)
            {
                [SVProgressHUD showSuccessWithStatus:@"支付完成"];
                NSArray *ctrs = weakSelf.navigationController.viewControllers;
                [weakSelf.navigationController popToViewController:ctrs[1] animated:YES];
            }
        }];

}

@end
